package net.lainiao.jwt.controller;

import net.lainiao.jwt.bean.Account;
import net.lainiao.jwt.bean.Login;
import net.lainiao.jwt.bean.ResponseData;
import net.lainiao.jwt.service.AccountService;
import net.lainiao.jwt.util.JWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/7/12.
 */
@RestController
@RequestMapping("/data")
public class DataControler {
    @Autowired
    AccountService accountService;

    @RequestMapping("/login")
    @ResponseBody
    public ResponseData  login(@RequestBody Login login) {
        Integer loginId=accountService.checkAccount(login);
        ResponseData responseData = ResponseData.ok();
        if(loginId!=null){
            login.setId(loginId);
            Account account=accountService.getAccountById(loginId);
            String token= JWT.sign(login,60L*30L*1000L);
            responseData.putDataValue("loginId",loginId);
            responseData.putDataValue("token",token);
            responseData.putDataValue("account",account);
        }
        else{
            responseData=responseData.customerError();
        }
        return responseData;
    }




}
